﻿package{
	
	
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
	
	
	public class Menu_LeaderBoard extends MovieClip{
		
		private var nickName:String;
		
		private var cubePoint:Number;
		
		public function Menu_LeaderBoard(){
			
			
			addEventListener(Event.ADDED_TO_STAGE, Init);
			
		}
		
		private function Init(e:Event) {
			removeEventListener(Event.ADDED_TO_STAGE, Init);
			
			
			
			
			
			Close.addEventListener(MouseEvent.CLICK, onClickClose);
			Send.addEventListener(MouseEvent.CLICK, onClickLeaderBoard);
			
			
			cubePoint = Clickr.cubeArray[0] * 1000 + Clickr.cubeArray[1] * 5000 + Clickr.cubeArray[2] * 10000 + Clickr.cubeArray[3] * 20000 + Clickr.cubeArray[4] * 60000;
			
			_NickTXT.restrict = "a-zA-Z0-9!@$*()_=\\-\\^";
			_NickTXT.maxChars = 20;
			
			//여기서 NickTXT가 Last Nickname값 받게
			
			
			addEventListener(Event.ENTER_FRAME, onEF);
			
			
			
			//nick은 영어, 숫자, 기호로만 20자 이내여야
			//hs는 최대값 체크 9,999,999
			//cp는 최대값 체크 9,504,000
			
			if (Clickr.bestScore > 9999999) {
				Clickr.bestScore = 9999999;
			}
			
			if (cubePoint > 9504000) {
				cubePoint = 9504000;
			}
			
			_HSTXT.text = Clickr.makeCurrency(Clickr.bestScore);
			_CPTXT.text = Clickr.makeCurrency(cubePoint);
			_NickTXT.text = Clickr.lastNickName;
			
			stage.focus = _NickTXT;
			_NickTXT.setSelection(_NickTXT.text.length,_NickTXT.text.length);
			//stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
		}
		
		private function onEF(e:Event = null){
			if (_NickTXT.length == 0) {
				Send.gotoAndStop("disable");
			}else {
				if(Send.currentFrame == 33)
				Send.gotoAndStop("Normal");
			}
		}
		
		private function onClickClose(e:MouseEvent) {
			trace(this, ": onClickClose");
			//stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKey);
			dispatchEvent(new NavigationEvent(NavigationEvent.CLOSE, "board", this));
			
		}
		
		private function onClickLeaderBoard(e:MouseEvent):void {
			trace(this, ": onClickSend&View");
			//NickTXT shouldn't be empty
			
			if (_NickTXT.length == 0) return;
			
			var sendingURL:String = "http://puzzlelab.net/send.php?nick=" + _NickTXT.text + "&hs=" + Clickr.bestScore.toString() + "&cp=" + cubePoint.toString();
            
            var request:URLRequest = new URLRequest(sendingURL);
            
			Clickr.lastNickName = _NickTXT.text;
			
			MovieClip(root).ClickrLocalData.data.bestScore = Clickr.bestScore;
			MovieClip(root).ClickrLocalData.data.cubeArray = Clickr.cubeArray;
			MovieClip(root).ClickrLocalData.data.lastNickName = Clickr.lastNickName;
			
			MovieClip(root).ClickrLocalData.flush();
			//리더보드 전송할 때 마지막닉네임값 저장
			
			
            try {
                navigateToURL(request);
            }catch (e:Error) {
                // handle error here
            }
			
			//잘 보냈다는 소리라도 있으면?
			
			dispatchEvent(new NavigationEvent(NavigationEvent.CLOSE, "board", this));
			
			
		}
		
	}
	
	
}